// $Rev: 130 $

#ifndef OUTGOING_CONNECTION_H
#define OUTGOING_CONNECTION_H

#include <deque>
#include <vector>
#include <string>
#include <poll.h>
#include "connection.h"
#include "server.h"

class OutgoingConnection : public Connection {
	public:
		OutgoingConnection(Server *server, std::string address, int socket,
		                   pollfd *poll_info);
		void process_events();
		void enqueue_file(const std::string md5, const Source *source);
		~OutgoingConnection();
	private:
		void process_next_file();
		enum FSMState {SENDING_MESSAGE, AWAITING_REPLY};
		void process_reply(const std::string &reply);
		void change_state(FSMState newstate);
		FSMState state;
		std::string command, buffer;
		Server *server;
		std::string address;
		std::deque<std::pair<std::string, std::vector<const Source *> > > queue;
		File *file;
		enum ReplyState {AWAITING_ERROR_CODE, AWAITING_NAME, AWAITING_SIZE, AWAITING_FRAGMENT_MD5};
		ReplyState reply_state;
		// No permitimos operadores de copia o asignacion para esta clase.
		OutgoingConnection(const OutgoingConnection &ic);
		OutgoingConnection &operator=(const OutgoingConnection &ic);
};

#endif
